package com.api.gbhg.daiyanlu.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.daiyanlu.Dao.PrintCopyActionDao;
import com.api.gbhg.luoao.utils.PageEntity;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 打印纸复印纸用量
 * Mr.dyl
 */
public class PrintCopyActionApi {
    private PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
    @POST
    @Path("/getPrintCopy")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getPrintCopy(Map<String,String> param){
        String requestId = param.get("requestId");
//        String status =  param.get("status");//状态
//        Integer pageNum= Integer.valueOf(param.get("pageNum"));//页数
//        Integer pageSize= Integer.valueOf(param.get("pageSize"));//显示数量
        PrintCopyActionDao dao = new PrintCopyActionDao();
        RecordSetTrans mainTable = dao.getMainTable(requestId);//获取主表数据
        PageEntity page = new PageEntity();
//        page.setPageNumber(pageNum);
//        page.setpageSize(pageSize);
        int total = dao.getMainTableCount(requestId);
        page.setTotal(total);//数据总条数
//        page.setPages((total + pageSize - 1) / pageSize);

        Map<String,Object>[] map=new HashMap[mainTable.getArray().size()];
        int i=0;
        while (mainTable.next()){
            map[i]=new HashMap<>();
            map[i].put("lcid",mainTable.getInt("lcid"));//流程id
            map[i].put("lcjlsqjlid",mainTable.getInt("lcjlsqjlid"));  //流程记录申请记录id
            map[i].put("xmmin",mainTable.getString("xmmin"));//项目名称
            RecordSet sbdw = rmcd.getSBDWById(mainTable.getInt("sbdw"));
            sbdw.next();
            map[i].put("sbdw",sbdw.getString("SUBCOMPANYNAME")); // 申报单位
            RecordSet sbks = rmcd.getSBKSById(mainTable.getInt("sbks"));
            sbks.next();
            map[i].put("sbks",sbks.getString("departmentmark"));// 申报科室
            RecordSet jbr = rmcd.getUserNameById(mainTable.getInt("jbr"));
            jbr.next();
            String loginid = jbr.getString("loginid");
            if(loginid==""){
                loginid="系统管理员";
            }
            map[i].put("jbr",loginid);  //编报人
            BigDecimal hwgz=new BigDecimal("0.00");
            BigDecimal gc=new BigDecimal("0.00");
            BigDecimal fw=new BigDecimal("0.00");
            BigDecimal xecg=new BigDecimal("0.00");

            if (!mainTable.getString("hwgz").equals("")){
                hwgz=hwgz.add(new BigDecimal(mainTable.getDouble("hwgz")));
            }
            if (!mainTable.getString("gc").equals("")){
                gc=gc.add(new BigDecimal(mainTable.getDouble("gc")));
            }
            if (!mainTable.getString("fw").equals("")){
                fw=fw.add(new BigDecimal(mainTable.getDouble("fw")));
            }
            if (!mainTable.getString("xecg").equals("")){
                xecg=xecg.add(new BigDecimal(mainTable.getDouble("xecg")));
            }
            map[i].put("zfcgyswy",hwgz.add(gc).add(fw).add(xecg).toString());//政府采购预算
            map[i].put("hwgz", hwgz.toString());//货物购置
            map[i].put("gc", gc.toString());//修缮工程
            map[i].put("fw",fw.toString());//服务预算
            map[i].put("xecg", xecg.toString());//小额采购
            RecordSet rs = dao.getDetailedTable(mainTable.getString("requestid"), 1);
            BigDecimal jewy1=new BigDecimal("0.00");
            BigDecimal jewy2=new BigDecimal("0.00");
            BigDecimal jewy3=new BigDecimal("0.00");
            BigDecimal je=new BigDecimal("0.00");

            while (rs.next()){
                jewy1=jewy1.add(new BigDecimal(rs.getDouble("jewy1")));
                jewy2=jewy2.add(new BigDecimal(rs.getDouble("jewy2")));
                jewy3=jewy3.add(new BigDecimal(rs.getDouble("jewy3")));
                je=je.add(new BigDecimal(rs.getDouble("je")));
            }
            map[i].put("jewy1",jewy1.toString());
            map[i].put("jewy2",jewy2.toString());
            map[i].put("jewy3",jewy3.toString());
            map[i].put("je",je.toString());//金额
            map[i].put("edzs",mainTable.getString("yszje"));//额度总数
            i++;
        }
        page.setList(map);
        page.setFromtable("uf_fyzdyzylxqbbb");
        return  JSON.toJSONString(page);
    }

    public String getPrintCopy1(String requestId){
//        String requestId = param.get("requestId");
//        String status =  param.get("status");//状态
//        Integer pageNum= Integer.valueOf(param.get("pageNum"));//页数
//        Integer pageSize= Integer.valueOf(param.get("pageSize"));//显示数量
        PrintCopyActionDao dao = new PrintCopyActionDao();
        RecordSetTrans mainTable = dao.getMainTable(requestId);//获取主表数据
        PageEntity page = new PageEntity();
//        page.setPageNumber(pageNum);
//        page.setpageSize(pageSize);
        int total = dao.getMainTableCount(requestId);
        page.setTotal(total);//数据总条数
//        page.setPages((total + pageSize - 1) / pageSize);

        Map<String,Object>[] map=new HashMap[mainTable.getArray().size()];
        int i=0;
        while (mainTable.next()){
            map[i]=new HashMap<>();
            map[i].put("lcid",mainTable.getInt("lcid"));//流程id
            map[i].put("lcjlsqjlid",mainTable.getInt("lcjlsqjlid"));  //流程记录申请记录id
            map[i].put("xmmin",mainTable.getString("xmmin"));//项目名称
            RecordSet sbdw = rmcd.getSBDWById(mainTable.getInt("sbdw"));
            sbdw.next();
            map[i].put("sbdw",sbdw.getString("SUBCOMPANYNAME")); // 申报单位
            RecordSet sbks = rmcd.getSBKSById(mainTable.getInt("sbks"));
            sbks.next();
            map[i].put("sbks",sbks.getString("departmentmark"));// 申报科室
            RecordSet jbr = rmcd.getUserNameById(mainTable.getInt("jbr"));
            jbr.next();
            String lastname = jbr.getString("lastname");
            if(lastname==""){
                lastname="系统管理员";
            }
            map[i].put("jbr",lastname);  //编报人
            BigDecimal hwgz=new BigDecimal("0.00");
            BigDecimal gc=new BigDecimal("0.00");
            BigDecimal fw=new BigDecimal("0.00");
            BigDecimal xecg=new BigDecimal("0.00");

            if (!mainTable.getString("hwgz").equals("")){
                hwgz=hwgz.add(new BigDecimal(mainTable.getDouble("hwgz")));
            }
            if (!mainTable.getString("gc").equals("")){
                gc=gc.add(new BigDecimal(mainTable.getDouble("gc")));
            }
            if (!mainTable.getString("fw").equals("")){
                fw=fw.add(new BigDecimal(mainTable.getDouble("fw")));
            }
            if (!mainTable.getString("xecg").equals("")){
                xecg=xecg.add(new BigDecimal(mainTable.getDouble("xecg")));
            }
            map[i].put("zfcgyswy",hwgz.add(gc).add(fw).add(xecg).divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//政府采购预算
            map[i].put("hwgz", hwgz.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//货物购置
            map[i].put("gc", gc.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//修缮工程
            map[i].put("fw",fw.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//服务预算
            map[i].put("xecg", xecg.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//小额采购
            RecordSet rs = dao.getDetailedTable(mainTable.getString("requestid"), 1);
            BigDecimal jewy1=new BigDecimal("0.00");
            BigDecimal jewy2=new BigDecimal("0.00");
            BigDecimal jewy3=new BigDecimal("0.00");
            BigDecimal je=new BigDecimal("0.00");

            while (rs.next()){
                jewy1=jewy1.add(new BigDecimal(rs.getDouble("jewy1")));
                jewy2=jewy2.add(new BigDecimal(rs.getDouble("jewy2")));
                jewy3=jewy3.add(new BigDecimal(rs.getDouble("jewy3")));
                je=je.add(new BigDecimal(rs.getDouble("je")));
            }
            map[i].put("jewy1",jewy1.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));
            map[i].put("jewy2",jewy2.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));
            map[i].put("jewy3",jewy3.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));
            map[i].put("je",je.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//金额

            BigDecimal yszje = new BigDecimal(mainTable.getString("yszje"));
            map[i].put("edzs",yszje.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//额度总数
            i++;
        }
        page.setList(map);
        page.setFromtable("uf_fyzdyzylxqbbb");
        return  JSON.toJSONString(page);
    }
}
